xquery version "0.9-ml"

(:if the attribute does not exist, this fails :)


let $arg_root := xdmp:get-request-field("root")
let $arg_node := xdmp:get-request-field("node")
let $arg_id := xdmp:get-request-field("id")
let $arg_att := xdmp:get-request-field("att")
let $arg_val := xdmp:get-request-field("val")

(:these variables are for testing :)
(:
let $arg_root := "doc('wines.xml')/wines/wine"
let $arg_node := "vintage"
let $arg_id := "10092"
let $arg_att := "dateModified"
let $arg_val := "currentdatetime"
:)

(: parse the node. if there is no node, use only the root node :)
let $parsed_node := if ($arg_node = "")
then "."
else $arg_node

(: parse the value that the attribute wil be set to. If it is a specific string, do a function instead
this is creating a reserved word 'currentdatetime':)
let $parsed_val := if ($arg_val = "currentdatetime")
then xs:string(fn:current-dateTime())
else $arg_val


let $s := concat("define variable $ext1 as xs:string external","
				 ",

				 "let $root := ",$arg_root," 
				 ",
				 
				 "for $r in $root 
				 ",

				 " where $r/@id=",$arg_id, "
				 ",

				 " return 
				 ",

				 " xdmp:node-replace($r/",$parsed_node,"/@", $arg_att, ", attribute ", $arg_att, " { $ext1 } )"

				  )
return
xdmp:eval($s, (xs:QName("ext1"), $parsed_val)),
<result>true</result>
